<template>
	<view class="login">
		<view class="icon-box">
			<image src="../../static/icos/default-face.png" class="icon"></image>
		</view>
		<form @submit="submit" @reset="">
			<view class="form">
				<view class="user-box">
					<text class="word">账号</text>
					<input type="text" name="user" class="input" placeholder="请输入账号"/>
				</view>
				<view class="pass-box">
					<text class="word">密码</text>
					<input type="password" name="pass" class="input" placeholder="请输入密码"/>
				</view>
				<button form-type="submit" type="primary" class="btn" >注册/登录</button>
			</view>
		</form>
		
		<view class="wx-box">
			<view class="wx-subtitle">第三方账号登录</view>
			<!-- #ifdef MP-WEIXIN -->
			<image src="../../static/icos/weixin.png"></image>
			<button open-type="getUserInfo" @getuserinfo="wxLogin" class="wxLogin-btn"></button>
			<!-- #endif -->
			<!-- #ifdef APP-PLUS || H5 -->
			<image src="../../static/icos/weixin.png" @click="appLogin('weixin')"></image>
			<image src="../../static/icos/QQ.png" @click="appLogin('qq')" ></image>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			submit(e){
				let me=this;
				let value=e.detail.value
				console.log(value)
				uni.showLoading({
					title:"登录中"
				})
				uni.request({
					method:"POST",
					header:{
						"content-type":"application/x-www-form-urlencoded"
					},
					url:`${me.root}/api/login`,
					data:value,
					success(res) {
						if(res.data.msg=="登录成功"){
							uni.showToast({
								title:res.data.msg,
								duration:1000,
								mask:true
							})
							uni.setStorageSync("user",res.data.data)
							uni.switchTab({
								url:'../me/me'
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								duration:1000,
								mask:true,
								image:'/static/icos/error.png'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			wxLogin(e){
				uni.showLoading({
					title:"登录中"
				})
				let {nickName,avatarUrl}=e.detail.userInfo
				// console.log(nickName,avatarUrl)
				let me=this
				uni.login({
					provider:"weixin",
					success(loginRes) {
						let {code} = loginRes;
						console.log(code,nickName,avatarUrl)
						uni.request({
							url:me.root+'/api/weixinLogin',
							method:"POST",
							header:{
								"content-type":"application/x-www-form-urlencoded"
							},
							data:{
								code,
								nickname:nickName,
								faceImg:avatarUrl
							},
							success(res) {
								if(res.data.msg=="登录成功"){
									uni.showToast({
										title:res.data.msg,
										duration:1000,
										mask:true
									})
									uni.setStorageSync("user",res.data.data)
									uni.switchTab({
										url:'../me/me'
									})
								}
							},
							complete() {
								uni.hideLoading()
							}
						})
						
					}
				})
			},
			// #ifdef APP-PLUS
			appLogin(provider){
				let me=this

				uni.login({
					provider:provider,
					success(loginRes) {
						uni.showLoading({
							title:"登陆中"
						})
						uni.getUserInfo({
							provider,
							success(res) {
								let info=res.userInfo;
								let openId=""
								let nickname=""
								let faceImg=""
								if(provider=="weixin"){
									openId=info.openId
									nickname=info.nickName
									faceImg=info.avatarUrl
								}else if(provider=="qq"){
									openId=info.openId
									nickname=info.nickname
									faceImg=info.figureurl_qq_2
								}
								uni.request({
									url:me.root+'/api/appLogin',
									method:"POST",
									header:{
										"content-type":"application/x-www-form-urlencoded"
									},
									data:{
										openid:openId,
										nickname,
										faceImg
									},
									success(res) {
										if(res.data.msg=="登录成功"){
											uni.showToast({
												title:res.data.msg,
												duration:1000,
												mask:true
											})
											uni.setStorageSync("user",res.data.data)
											uni.switchTab({
												url:'../me/me'
											})
										}
									},
									complete() {
										uni.hideLoading()
									}
								})
								
								
							}
						})
					}
				})
			},
			// #endif
			appLogin(){
				let me=this
				let flag=confirm("是否下在App")
				if(flag){
					uni.navigateTo({
						url:"/pages/downLoad/downLoad"
					})
				}
			}
		}
	}
</script>

<style scoped>
@import url("login.css");
</style>
